package com.wyx.bbs.controller;

import com.wyx.bbs.domain.*;
import com.wyx.bbs.service.ArticleService;
import com.wyx.bbs.util.OSSClientUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 文章
 * @Date: Created in 11:20 2021/3/31
 * @Modefied By:
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    ArticleService articleService;
    @Resource
    OSSClientUtil ossUtil;
//    @Resource
//    Article article;


Map map = new HashMap();
private Object resultMap(int code, String msg){
    map.put("code",code);
    map.put("msg",msg);
    return map;
}

/**
 * 发表帖子
 * @param
 * @return
 */
@RequestMapping(value = "/add",method = RequestMethod.POST)
@ResponseBody
public Object addPost (@RequestBody Article article){

    int uid = article.getUid();
    String post = article.getPost();



    if(uid!=0 && post!=null){

        int adds = articleService.addPost(article);
        if(adds!=0){
            resultMap(200, "发表成功");
        }else{
            resultMap(-1, "发表失败");
        }
    }else{
        resultMap(-1, "必填项为空");
    }
    return map;
}

/**
 * 查看全部
 * @return
 */
@RequestMapping("/selectAll")
@ResponseBody
public Object selectAll(@RequestParam Integer uid){

    System.out.println("uid:"+uid);
    List<ArticleToUser> list = articleService.getArticleAll(uid);

    if(list!=null){
        resultMap(200,"查询成功");
        map.put("list",list);
    }else{
        resultMap(-1,"查询失败");
    }
    return map;
}




/**
 * 根据postId查看
 * @return
 */
@RequestMapping("/getByPostId")
@ResponseBody
public Object getByPostId(@RequestParam Integer postId, @RequestParam Integer uid){

    if(postId!=0){
        List<ArticleToUser> list = articleService.getByPostId(postId, uid);

        if(list!=null){
            resultMap(200,"查询成功");
            map.put("list",list);
        }else{
            resultMap(-1,"查询失败");
        }
    }else{
        resultMap(-1,"查询失败,uid为空");
    }
    return map;
}


/**
 * 根据uid查看
 * @return
 */
@RequestMapping("/getByUid")
@ResponseBody
public Object getByUid(@RequestParam Integer uid, @RequestParam Integer selfId){
    System.out.println("uid:"+uid);
    if(uid!=0){
        List<ArticleToUser> list = articleService.getByUid(uid, selfId);
        List<ArticleToUser> result = new ArrayList<>();
        User user = new User();
        ArticleToUser result1 = new ArticleToUser();
        if(list!=null){
            for(ArticleToUser list1 : list){

                user.setAvatarUrl(list1.getAvatarUrl());
                user.setNikeName(list1.getNikeName());

                list1.setUser(user);

            }


            resultMap(200,"查询成功");
            map.put("list",list);
        }else{
            resultMap(-1,"查询失败");
        }
    }else{
        resultMap(-1,"查询失败,uid为空");
    }
    return map;
}


    /**
     * 根据id删除
     * @return
     */
    @RequestMapping("/delArticle")
    @ResponseBody
    public Object delArticle(@RequestParam Integer id){
        if(id!=0){
            int i = articleService.delArticle(id);
            if(i!=0){
                resultMap(200,"删除成功");
            }else{
                resultMap(-1,"删除失败");
            }
        }else{
            resultMap(-1,"删除失败,id为空");
        }
        return map;
    }

}
